package com.homedo.microservice.open.service.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.dao.bean.po.enhanced.base.BaseEnhanced;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.bean.po.RouteService;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.RouteServiceEnhanced;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.RouteServiceUrlEnhanced;
import com.homedo.microservice.open.service.gateway.persistence.dao.RouteServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月18日 11时30分20秒
 */
@Service
public class RouteServiceService extends BaseService<RouteService> {
    @Autowired
    private RouteServiceDao routeServiceDao;
    @Autowired
    private RouteServiceUrlService routeServiceUrlService;


    @Override
    public List<RouteServiceEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<RouteService> routeServices = routeServiceDao.getListByIds(ids, RouteService.class);

        if (!Detect.notEmpty(routeServices)) {
            return null;
        }
        List<RouteServiceEnhanced> routeServiceEnhanceds = this.convent2Enhanceds(routeServices);

        this.appendEnhancedCommons(routeServiceEnhanceds, userContext);

        return routeServiceEnhanceds;
    }


    @Override
    public List<RouteServiceEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<RouteService> routeServices = routeServiceDao.getListByIdsGreaterThanMark0(ids, RouteService.class);

        if (!Detect.notEmpty(routeServices)) {
            return null;
        }
        List<RouteServiceEnhanced> routeServiceEnhanceds = this.convent2Enhanceds(routeServices);

        this.appendEnhancedCommons(routeServiceEnhanceds, userContext);

        return routeServiceEnhanceds;
    }


    @Override
    public List<RouteServiceEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<RouteService> routeServices = (List<RouteService>) pos;
        List<RouteServiceEnhanced> routeServiceEnhanceds = Lists.newArrayList();
        routeServices.forEach(e -> routeServiceEnhanceds.add(new RouteServiceEnhanced(e)));
        return routeServiceEnhanceds;
    }

    public List<RouteServiceEnhanced> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
        List<RouteServiceEnhanced> routeServiceEnhanceds = (List<RouteServiceEnhanced>) enhancedItems;

        this.appendRouteServiceUrlEnhanceds(routeServiceEnhanceds, userContext);

        return routeServiceEnhanceds;
    }

    public List<RouteServiceEnhanced> appendRouteServiceUrlEnhanceds(List<RouteServiceEnhanced> routeServiceEnhanceds, UserContext userContext) {
        List<String> serviceIds = routeServiceEnhanceds.stream().map(RouteServiceEnhanced::getId).collect(Collectors.toList());

        List<RouteServiceUrlEnhanced> routeServiceUrlEnhanceds = routeServiceUrlService.getEnhanceds4Services(serviceIds, userContext);

        if (!Detect.notEmpty(routeServiceUrlEnhanceds)) {
            return routeServiceEnhanceds;
        }

        this.appendRouteServiceUrlEnhanceds(routeServiceEnhanceds, routeServiceUrlEnhanceds, userContext);

        return routeServiceEnhanceds;
    }

    private void appendRouteServiceUrlEnhanceds(List<RouteServiceEnhanced> routeServiceEnhanceds, List<RouteServiceUrlEnhanced> routeServiceUrlEnhanceds, UserContext userContext) {
        routeServiceEnhanceds.forEach(e -> {
            this.appendRouteServiceUrlEnhanceds(e, routeServiceUrlEnhanceds, userContext);
        });
    }

    private void appendRouteServiceUrlEnhanceds(RouteServiceEnhanced routeServiceEnhanced, List<RouteServiceUrlEnhanced> routeServiceUrlEnhanceds, UserContext userContext) {
        List<RouteServiceUrlEnhanced> curRouteServiceUrlEnhanceds = Lists.newArrayList();

        routeServiceUrlEnhanceds.forEach(e -> {
            if (e.getRouteServiceEnhanced().getId().equals(routeServiceEnhanced.getId())) {
                curRouteServiceUrlEnhanceds.add(e);
            }
        });

        routeServiceEnhanced.setRouteServiceUrlEnhanceds(curRouteServiceUrlEnhanceds);
    }
}
